
clear
set more off
capture log close
set seed 12345
set matsize 5000
set maxvar 20000	
* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"

 
* load analysis sample
use $DATA/reg_file_firm, clear

forval y = `yrb' / `yre' {	
	gen th`y' = 0
}

*******************************************
** regressions
*******************************************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1

local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year"
local r2 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year"
local r3 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year"
local r4 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"


****************************
****************************
****************************
* 1) main analysis across control groups
*	Figure 2
*	Table 2
*	Figure A.2, Panel A
****************************
****************************
****************************

* Event-study regressions 
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

* Main regressions
foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 3 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table2_panelA_itt.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/figure2_panelB.csv", replace se ar2 mtitles( ) keep(t20* ) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/figure_A2_panelA.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

* for Panel A, control trends rel to 2012 
foreach o in wages_ft {
	eststo ra: quietly areg `rlog'`o' t`yrb'-t2011 t`bp1'-t`yre' y`yrb'-y2011 y`bp1'-y`yre' `t'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0), absorb(firm_tin) vce(cluster firm_tin)
	esttab using "$OUTPUT/figure2_panelA_ctrl.csv", replace se ar2 mtitles( ) keep(t20* y20*) 
	eststo clear
}

**
**  2sls main results
***
* 1) first stage tables 
foreach tt in tt1112 {
preserve
	keep if `tt'==1
	collapse (count) n=firm_tin (sum) t1 ,by(year)
	gen sh=t1/n
	gen trt=1
	gen tvc = "`tt'"
	tempfile te1_`tt'
	save `te1_`tt'', replace
restore
preserve
	keep if `tt'==0
	collapse (count) n=firm_tin (sum) t1 ,by(year)
	gen sh=t1/n
	gen trt=0
	gen tvc = "`tt'"
	tempfile te0_`tt'
	save `te0_`tt'', replace
restore
}
preserve
	use `te1_tt1112', clear
	gen zz=1
	foreach tt in tt1112 {
		append using `te1_`tt''
		append using `te0_`tt''
	}
	drop if zz==1
	drop zz
	outsheet using $OUTPUT/figure2_panelC.csv, comma replace
restore

* 2) 2sls Regressions
local tt "tt1112"
local tout "t1"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0)
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 1 2 3 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure2_panelD.csv", replace se ar2 mtitles( ) keep(th20*) addnote("Each specification includes a different set of control variabels")
esttab using "$OUTPUT/table2_panelA_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**
**  Elasticity (ITT) 
***
local trt "tt1112_ncg"

local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 3 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table2_panelA_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



****************************
****************************
****************************
** Robustness 
	* Table 2
	* Figure A.2, panel B
	* Figure A.3 (single owner)
	* Figure A.5
	* Table A.6
****************************
****************************
****************************

**************************************************************************************************
**************************************************************************************************
**  robust - Single owners [Table 2, Table A.6, Fig A.3]
**************************************************************************************************
**************************************************************************************************
* For Table 2 and Figure A.3
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 3 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & own1==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table2_panelB_1own.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/figure_A3_panelB.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

* for Panel A, rel to 2012 
foreach o in wages_ft {
	eststo ra: quietly areg `rlog'`o' t`yrb'-t2011 t`bp1'-t`yre' y`yrb'-y2011 y`bp1'-y`yre' `t'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & own1==1, absorb(firm_tin) vce(cluster firm_tin)
	esttab using "$OUTPUT/figure_A3_panelA_ctrl.csv", replace se ar2 mtitles( ) keep(t20* y20*) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

**
**  2sls main - single owner
***
* 1) first stage tables
foreach tt in tt1112 {
preserve
	keep if `tt'==1
	keep if own1==1
	collapse (count) n=firm_tin (sum) t1 ,by(year)
	gen sh=t1/n
	gen trt=1
	gen tvc = "`tt'"
	tempfile te1_`tt'
	save `te1_`tt'', replace
restore
preserve
	keep if `tt'==0
	keep if own1==1
	collapse (count) n=firm_tin (sum) t1 ,by(year)
	gen sh=t1/n
	gen trt=0
	gen tvc = "`tt'"
	tempfile te0_`tt'
	save `te0_`tt'', replace
restore
}
preserve
	use `te1_tt1112', clear
	gen zz=1
	foreach tt in tt1112 {
		append using `te1_`tt''
		append using `te0_`tt''
	}
	drop if zz==1
	drop zz
	outsheet using $OUTPUT/figure_A3_panelC.csv, comma replace
restore

* 2) 2sls 
local tt "tt1112"
local tout "t1"
forval y=2008/2016 {
	reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0) & own1==1
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0) & own1==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure_A3_panelD.csv", replace se ar2 mtitles( ) keep(th20*) addnote("Each specification includes a different set of control variabels")
esttab using "$OUTPUT/table_A6_1own_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "tt1112_ncg"
local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & own1==1 & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_1own_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



***************************************************************************************
***************************************************************************************
**  Robust - All top  [Table 2, Table A.6]
***************************************************************************************
***************************************************************************************

gen vv = 0 
replace vv = t2 if year==2011
bys firm_tin: egen t2_2011 = sum(vv)
drop vv

gen t2t1112=.
replace t2t1112=1 if t2_2011==1 & t2_2012==1
replace t2t1112=0 if t2_2011==0 & t2_2012==0
replace t2t1112 = . if t2t1112==0 & tt1112==1
replace t2t1112 = . if t2t1112==1 & tt1112==0
tab year t2t1112

local t "t2t1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in  1 2 3 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table2_panelB_topall.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}

*********
**  2sls main 
*********
* 3) 2sls 
local tt "t2t1112"
local tout "t2"
forval y=2008/2016 {
	quietly reg `tout' `tt' if year==`y' & (`tt'==1 | `tt'==0) 
	predict hat`y',xb
}
forval y = `yrb' / `yre' {
	drop th`y'
	gen th`y' = 0
	replace th`y' = . if hat`y'==.
	replace th`y' = hat`y' if year==`y'
}

foreach r in 4 {
	eststo r`r': quietly reghdfe lnwages_ft th2008 th2009 th2010 th2013 th2014 th2015 th2016 y2008 y2009 y2010 y2013 y2014 y2015 y2016 `tt'  [aw=n_ft_lower_2012]  if (`tt'==1 | `tt'==0)  & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table_A6_topall_2sls.csv", replace se ar2 mtitles( ) keep(th2016) addnote("Each specification includes a different set of control variabels")
eststo clear
drop hat*

**********
**  Elasticity
**********
local trt "t2t1112"
local t "dlnmtr_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in  4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0) & (year==2011 | year==2012 | year==2016), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table_A6_topall_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



*************************************************************************
*************************************************************************
**  Robustness - control firms over 200K [Table 2, Fig. A.5]
*************************************************************************
*************************************************************************
local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 3 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if ptop==1, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}		
	esttab using "$OUTPUT/table2_panelB_top_v_top2.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/figure_A5_top_v_top2.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



****************************
****************************
****************************
** Placebo
	* Table 2
	* Figure A.5
****************************
****************************
****************************

*************************************************************************
*************************************************************************
**  Placebo tests 2008 treat [Table 2]
*************************************************************************
*************************************************************************
* 2008-2011
foreach n in t1 n_ft_lower netinc valaddpw_ft naics cz {
	gen zz = 0
	replace zz = `n' if year==2008
	bys firm_tin: egen `n'_2008=sum(zz)
	drop zz
}

foreach i in netinc_2008 valaddpw_ft_2008 {
	xtile `i'cat = `i',nquantiles(10)
}

local baseyr "2008"
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year"
local r2 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year"
local r3 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year"
local r4 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"

local t "t1"
local trtyr "2008"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'_`trtyr'==.
	replace t`y' = `t'_`trtyr' if year==`y'
}
local bm1 = `trtyr'-1
local bp1 = `trtyr'+1
local events "t2009-t2012 y2009-y2012 `t'_`trtyr'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 3 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events' if year==2008 | year==2011 [aw=n_ft_lower_`trtyr'], absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/table2_panelC_placebo2008_`t'.csv", replace se ar2 mtitles( ) keep(t2011) addnote("Each specification includes a different set of control variabels")
	eststo clear
}
* reset base year
local baseyr "2012"



*************************************************************************
*************************************************************************
**  Placebo tests 350-450K v. 250-350K [Table 2, Fig A.5]
*************************************************************************
*************************************************************************
local baseyr "2012"
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year"
local r2 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year"
local r3 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year"
local r4 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"

local t "ptop3"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 2 3 4 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=n_ft_lower_2012] if ptop==1 & t1_2012==0, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}		
	esttab using "$OUTPUT/table2_panelC_top3_v_top2.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
	esttab using "$OUTPUT/figure_A5.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	eststo clear
}



*****************************
*****************************
*****************************
**  Firm
	* Figure 4
*****************************
*****************************
*****************************

local vs "revenue valaddpw_ft ded_nonwage" 
foreach w in 5 {
	local ptop = 100-`w'
	local pbot = `w'
	foreach o in `vs' {
		sort year `o'
		by year: egen zt=pctile(`o'), p(`ptop')
		by year: egen zb=pctile(`o'), p(`pbot')
		gen w`o'`ptop' = `o'
		replace w`o'`ptop' = zt if `o'>zt & `o'~=.
		replace w`o'`ptop' = zb if `o'<zb & `o'~=.
		drop zt zb
	}
}

foreach o in revenue valaddpw_ft ded_nonwage {
	gen ln`o'=ln(w`o'95) 
}

* ensure non-negative weights
gen zvaladdpw_ft_2012=valaddpw_ft_2012
replace zvaladdpw_ft_2012=0 if valaddpw_ft_2012<0

local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.state_`baseyr'##ib`baseyr'.year"
local r2 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.state_`baseyr'##ib`baseyr'.year"

local t "t1_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2011 t`bp1'-t`yre' y`yrb'-y2011 y`bp1'-y`yre' `t'"

foreach r in 1 {
	eststo r`r': quietly reghdfe lnvaladdpw_ft `events' [aw=zvaladdpw_ft_2012]  if (`t'==1 | `t'==0) , absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure4_valaddpw.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
eststo clear

foreach r in 2 {
	eststo r`r': quietly reghdfe lnrevenue `events' [aw=revenue_2012]  if (`t'==1 | `t'==0) , absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure4_revenue.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
eststo clear

foreach r in 2 {
	eststo r`r': quietly reghdfe lnded_nonwage `events' [aw=revenue_2012]  if (`t'==1 | `t'==0) , absorb(`r`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure4_ded_nonwage.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
eststo clear




clear
exit

